package cmu.mobile.lab;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SMS extends Activity {
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_sms);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		final Bundle extras = getIntent().getExtras();
//		final String imagePath = extras.getString("imagePath");
		final String imagePath = "/sdcard/image2.jpg";
		ImageView previewPicture = (ImageView)findViewById(R.id.itemPicture);
		final String store = extras.getString("store");
		final String price = extras.getString("price");


		if (imagePath != null) {
			// this should be different
			Uri u = Uri.parse(imagePath);
			previewPicture.setImageURI(u);
		}
		
		// update price and store text
		if (store != null) {
			((TextView)findViewById(R.id.StoreText)).setText("Store: " + store);
		}
		
		if (price != null) {
			((TextView)findViewById(R.id.PriceText)).setText("Price: " + price);
		}
		
		
		findViewById(R.id.CancelButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.SendButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				// get allll the info from extras
				String size = extras.getString("size");
				String comment = extras.getString("comment");
				String message = ((EditText)findViewById(R.id.MessageBox))
					.getText().toString();
				String ownerID = extras.getString("ownerid");
				String numbers = extras.getString("phoneNumbers");
				String[] numbersList = numbers.split(",");
				
				MMSContent mms = new MMSContent();
				mms.setStore(store);
				mms.setPrice(price);
				mms.setSize(size);
				mms.setItemComment(comment);
				mms.setMessageText(message);
				//mms.setReceiverID("9865");
				mms.setImagePath(imagePath);
				mms.setOwnerID(ownerID);
				
				// TODO: get item id from database
				int id = 123456;
				
				Database mDbHelper = new Database(SMS.this);
				mDbHelper.open();
//				//if there's mms.getImagePath is not null then,
//				if (mms.getImagePath()!=null){
//					mDbHelper.addItem(mms);
//				}
//				//if mms.getRating() is not null then,
//				if (mms.getRating()!=null){
//					mDbHelper.rateItem(1, mms);
//				}
//				
				//ArrayList<MMSContent> rating = mDbHelper.getRating(1);
//				ArrayList<MMSContent> friends= mDbHelper.getAllFriendsItems(); 
//				ArrayList<MMSContent> all = mDbHelper.getAllMyItems();
				//items.close();
				//Log.d("*nn**", "**************************************************");
//				mDbHelper.close();

				for (String number : numbersList) {
					//if there's mms.getImagePath is not null then,
					if (mms.getImagePath()!=null){
						mDbHelper.addItem(mms);
					}
					//if mms.getRating() is not null then,
					if (mms.getRating()!=null){
						mDbHelper.rateItem(1, mms);
					}

					mms.setReceiverID(number);
					sendSMS(mms.getReceiverID(), id, mms);	
				}
				mDbHelper.close();

				Toast.makeText(SMS.this, "Message Sent!", Toast.LENGTH_LONG).show();

				// home button or automatically go back after message is sent?
			}
		});
		
	}

	public static void sendSMS(String phoneNumber, int id, MMSContent item) {        
		SmsManager sms = SmsManager.getDefault();
		String identification = item.getOwnerID() + "Q" + id;
		ImageLoader.uploadItem(item.getImagePath(), item, identification);
		String message = SMSReceiver.FASHIONISTA_MESSAGE_PREFIX + id;
		sms.sendTextMessage(phoneNumber, null, message, null, null); 
	}    

}
